package com.ciberiasoluciones.cibervoto.servlet;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import com.ciberiasoluciones.cibervoto.utility.Cibervoto_sesion;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author IngJarava
 */
@WebServlet("/Servlet_Sesion")
public class Servlet_Sesion extends HttpServlet {

    private HttpSession             session_actual;
    private String                  text_eror = null;
    private String                  text_info_error = null;
    private PrintWriter             out;
    private Cibervoto_sesion        cv_sesion;
    private Integer visitCount      = new Integer(0);
    private String                  ID = "";
    
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
         
       synchronized(session_actual){
        try{
           if(cv_sesion.getProc_elec_Vigente())
            {
                System.out.print(" getProc_elec_Vigente() "+cv_sesion.getProc_elec_Vigente());
                if(cv_sesion.login())
                {
                    System.out.print(" cv_sesion.login() "+cv_sesion.login());
                    if(cv_sesion.getVotar())
                    {
                        System.out.print(" cv_sesion.getVotar() "+cv_sesion.getVotar());
                       response.sendRedirect("info/infovoto.xhtml");   
                    }
                    else
                    {
                         System.out.print(" Tarjeton ");
                        session_actual.setAttribute("codmiembro", String.valueOf(cv_sesion.getCodigoMiembroPK()));
                        session_actual.setAttribute("codproelectoral", String.valueOf(cv_sesion.getCodProcesoElectoralPK()));
                        response.sendRedirect("tarjeton.jsp");
                    } 
                }
                else
                {
                    text_info_error = "Error de Sesion";
                    text_eror = "Usted a digitado erroneamente su cedula y/o contraseña.\n" +
                                "<br/><strong>Vuelva a intentarlo.</strong>";
                    response.resetBuffer();
                    request.setAttribute("text_eror", text_eror);
                    request.setAttribute("text_info_error", text_info_error);
                    request.getRequestDispatcher("sesion.jsp").forward(request, response);
                }
             }
             else
             {
                response.sendRedirect("info/noproelectoral.jsp");
             }
            } catch(java.lang.Exception e)
            {
                    text_info_error = "Error de Sesion";
                    text_eror = "Usted a digitado erroneamente su cedula y/o contraseña.\n" +
                                "<br/><strong>Vuelva a intentarlo.</strong>";
                    response.resetBuffer();
                    request.setAttribute("text_eror", text_eror);
                    request.setAttribute("text_info_error", text_info_error);
                    e.getStackTrace();
                    
                    request.getRequestDispatcher("sesion.jsp").forward(request, response);
            } 
         finally {            
            out.close();
         }
        }
       
    }
      // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setBufferSize(8192 * 16);
        response.setContentType("text/html;charset=UTF-8");
        
        out                     = response.getWriter();
        session_actual          = request.getSession();
        synchronized (session_actual)
        {
            if(request.getParameter("cedula") != null ){
                if(request.getParameter("password").toString() != null ){
                    if(request.getParameter("cedula").matches("[0-9]+")){
                        //login
                        System.out.print(" login ");
                        cv_sesion = new Cibervoto_sesion();
                        cv_sesion.setUser(Integer.parseInt(request.getParameter("cedula").toString()));
                        cv_sesion.setPassword(request.getParameter("password").toString().toUpperCase());
                        processRequest(request, response);
                    }                       
                    else{
                        //cedula incorrecta
                        text_info_error = "Error de Sesion";
                        text_eror = "Usted a digitado erroneamente su cedula.\n" +
                                    "<br/><strong>Vuelva a intentarlo.</strong>";
                        response.resetBuffer();
                        request.setAttribute("text_eror", text_eror);
                        request.setAttribute("text_info_error", text_info_error);
                        System.out.print(" cedula incorrecta ");
                        request.getRequestDispatcher("sesion.jsp").forward(request, response);
                    }                        
                }else{
                    //debe introducir la contraseña 
                        text_info_error = "Error de Sesion";
                        text_eror = "Usted a digitado erroneamente su contraseña.\n" +
                                    "<br/><strong>Vuelva a intentarlo.</strong>";
                        response.resetBuffer();
                        request.setAttribute("text_eror", text_eror);
                        request.setAttribute("text_info_error", text_info_error);
                        System.out.print(" debe introducir la contraseña ");
                        request.getRequestDispatcher("sesion.jsp").forward(request, response);
                } 
            }else{
                //debe introducir cedula
                text_info_error = "Debe intruducir cedula";
                text_eror = "No se Aceptan campos vacios <br/> "
                        + "en las casilla de la cedula.";
                response.resetBuffer();
                request.setAttribute("text_eror", text_eror);
                request.setAttribute("text_info_error", text_info_error);
                System.out.print(" Debe intruducir cedula ");
                request.getRequestDispatcher("sesion.jsp").forward(request, response);
            }
        }
          
    }
    

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

  
}



/*
 if(session_actual.getAttribute("cod_elector").equals(ID)){
                                    text_info_error = "Error en Multiple Sesion";
                                    text_eror = "Usted ha iniciado sesion en otro quipo.";
                                    request.setAttribute("cedula", request.getParameter("cedula").toString());
                                    request.setAttribute("text_eror", text_eror);
                                    request.setAttribute("text_info_error", text_info_error);
                                    request.getRequestDispatcher("index.jsp").forward(request, response);
                                }
 */



/*
 if(request.getParameter("cedula") == null ){
             text_info_error = "Error en los Campos";
             text_eror = "Se esperan numeros<br/> "
                         + "en la Casilla de la Cédula.";
             request.setAttribute("cedula", "");
             request.setAttribute("text_eror", text_eror);
             request.setAttribute("text_info_error", text_info_error);
             response.resetBuffer();
             request.getRequestDispatcher("sesion.jsp").forward(request, response);
            }else{
             if(request.getParameter("password").equals("") || request.getParameter("password").toString() == null ){
                 if(request.getParameter("password").equals("") || 
                         request.getParameter("password").toString() == null
                         || request.getParameter("cedula").equals("")
                         || request.getParameter("cedula").toString() == null
                         ){
                      text_info_error = "Error en los Campos";
                      text_eror = "No se Aceptan letras o campos vacios <br/> "
                              + "en las casillas.";
                      request.setAttribute("cedula", "");
                      request.setAttribute("text_eror", text_eror);
                      request.setAttribute("text_info_error", text_info_error);
                      response.resetBuffer();
                      request.getRequestDispatcher("sesion.jsp").forward(request, response);
                     }
                  }else{
                    try{
                      cv_sesion = new Cibervoto_sesion();
                      cv_sesion.setUser(Integer.parseInt(request.getParameter("cedula").toString()));
                      cv_sesion.setPassword(request.getParameter("password").toString().toUpperCase());
                      }catch(NumberFormatException ex){
                      text_info_error = "Error de Sesion";
                      text_eror = "Usted a digitado erroneamente su cedula y/o contraseña.\n" +
                                  "<br/><strong>Vuelva a intentarlo.</strong>";
                      response.resetBuffer();
                      request.setAttribute("cedula", String.valueOf(cv_sesion.getUser()));
                      request.setAttribute("text_eror", text_eror);
                      request.setAttribute("text_info_error", text_info_error);
                      request.getRequestDispatcher("sesion.jsp").forward(request, response);
                    }
                 }
             }
 
 
 */